<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input
            placeholder="请输入内容"
            class="input-with-select"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table
        :data="goodsList"
        border
        stripe
        @row-click="clickTable"
        ref="refTable"
      >
        <el-table-column label="展开" type="expand">
          <template slot-scope="scope">
            <div
              v-html="scope.row.goods_introduce"
              style="height: 550px; overflow: scroll"
            />
          </template>
        </el-table-column>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column
          label="商品价格(元)"
          prop="goods_price"
          width="95px"
        ></el-table-column>
        <el-table-column
          label="商品重量"
          prop="goods_weight"
          width="95px"
        ></el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="140px">
          <template slot-scope="scope">{{
            scope.row.add_time | dataformat
          }}</template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <el-tooltip
              effect="dark"
              content="编辑"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click.stop="goEditPage(scope.row.goods_id)"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              effect="dark"
              content="删除"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click.stop="deleteGoods(scope.row.goods_id)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页功能 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 6, 8, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 获取用户信息的查询参数
      queryInfo: {
        // 查询内容
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页展示多少条数据
        pagesize: 10
      },
      // 总条目数
      total: 0,
      // 获取的商品数据
      goodsList: [],
      // 商品描述
      goodsIn: '123'
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    // 获取商品数据
    async getGoodsList () {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error('获取商品列表数据失败')
      // console.log(res)
      this.goodsList = res.data.goods
      this.total = res.data.total
      // console.log(this.goodsList)
      // 每个数据添加商品描述属性
      this.goodsList.forEach(async item => {
        // console.log(item)
        item.goods_introduce = await this.getGoodsIntroduce(item.goods_id)
      })
    },
    // 修改每页展示数据数量
    handleSizeChange (num) {
      this.queryInfo.pagesize = num
      this.getGoodsList()
    },
    // 修改当前页码
    handleCurrentChange (num) {
      this.queryInfo.pagenum = num
      this.getGoodsList()
    },
    // 删除商品数据
    async deleteGoods (id) {
      // this.$confirm返回的是一个Promise对象
      const result = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 捕捉错误并且返回这个错误
      // 点击取消返回一个cancel字符串
      // 点击确认返回一个confirm字符串
      if (result !== 'confirm') {
        return this.$message('你取消了删除')
      }
      const { data: res } = await this.$http.delete('goods/' + id)
      if (res.meta.status !== 200) return this.$message.error('删除用户失败')
      this.$message.success('删除商品成功')
      this.getGoodsList()
    },
    // 点击跳转到添加商品页面
    goAddPage () {
      this.$router.push('/goods/add')
    },
    // 点击跳转到编辑商品页面
    goEditPage (id) {
      // console.log(id)
      this.$router.push({
        name: 'goods_edit',
        params: {
          id: id
        }
      })
    },
    // 点击行，直接展开行
    clickTable (row, index, e) {
      // console.log(row, index, e)
      this.$refs.refTable.toggleRowExpansion(row)
    },
    // 获取商品描述
    async getGoodsIntroduce (id) {
      // console.log(row)
      const { data: res } = await this.$http.get(`goods/${id}`)
      if (res.meta.status !== 200) return this.$message.error('展开数据获取失败！')
      // console.log(res.data.goods_introduce)
      return res.data.goods_introduce
    }
  }
}
</script>

<style scope lang="less">
.el-table__expanded-cell {
  padding: 0 !important;
}
</style>
